<?php
namespace Admin\Service;

use Think\Service\BasicService;

class BookService extends BasicService {
    public function insert($model) {
        return $this->db->add($model);
    }

    public function AddAll($model) {
        $data = $this->db->addAll($model);
        return $data;
    }

    public function remove($condition) {
        return $this->db->where($condition)->delete();
    }

    public function update($where, $model = array()) {
        return $this->db->where($where)->save($model);
    }

    public function find($condition) {
        return $this->db->where($condition)->find();
    }

    public function query($condition, $limit = array(), $field = '*', $order = '', $join = '') {
        $data = $this->db->join($join)->where($condition)->field($field)->limit($limit['start'], $limit['end'])->order($order)->select();
        return $data;
    }

    public function count($condition) {
        return $this->db->where($condition)->count();
    }

    /*
     * 获取书籍位置 old
     */
    public function GetWZ($ID) {
        $data = $this->db->field('ts_ck.*,ts_sj.CODE as SJCODE,ts_sg.CODE as SGCODE')->join('ts_sg  on ts_sg.BHCODE = ts_book.WZ_CODE')
            ->join('ts_sj on ts_sg.SJ_ID = ts_sj.BHCODE')->join('ts_ck on ts_ck.BHCODE = ts_sj.CK_ID')
            ->where(array('ts_book.ID' => $ID))->find();
        $data ? $str = $data['P'] . $data['C'] . $data['Z'] . $data['ADDRESS'] . $data['NAME'] . '书架代码:' . $data['SJCODE'] . '书柜代码:' . $data['SGCODE'] : $str = '书籍未入库';
        return $str;
    }
    /*
 * 获取书籍位置
     * book_id
 */
    public function GetWZ4($ID) {
        $data = $this->db->field('ts_ck.*,ts_sj.CODE as SJCODE,ts_sg.CODE as SGCODE')
            ->join('ts_sg  on ts_sg.BHCODE = ts_book.WZ_CODE')
            ->join('ts_sj on ts_sg.SJ_ID = ts_sj.BHCODE')
            ->join('ts_ck on ts_ck.BHCODE = ts_sj.CK_ID')
            ->where(array('ts_book.ID' => $ID))->find();
        return $data;
    }

    /*
     * 获取书籍位置 new
     * ts_sg.BHCODE 书柜code
     */
    public function GetWZ1($ID) {
        $data = $this->db->field('ts_ck.*,ts_sj.CODE as SJCODE,ts_sg.CODE as SGCODE,
            ts_sg.NAME as SGNAME,ts_sg.BHCODE as WZID,ts_ck.ID as CK_ID')
            ->join('ts_sj on ts_sg.SJ_ID = ts_sj.BHCODE')
            ->join('ts_ck on ts_ck.BHCODE = ts_sj.CK_ID')
            ->where(array('ts_sg.BHCODE' => $ID))->find();
        return $data;
    }

    /*
         * 获取书籍位置 new
         * ISBN 书籍ISBN
         */
    public function GetWZ2($ISBN) {
        $where['ts_book.ISBN'] = $ISBN;
        $where['ts_book_gg.NUM'] = array('gt',
            0);
        $data = $this->db->field('ts_book.TITLE,ts_book.ISBN,ts_ck.*,ts_sj.CODE
             as SJCODE,ts_sg.CODE as SGCODE,ts_sg.NAME
              as SGNAME,ts_sg.BHCODE as WZID,
              ts_wz.ID as WZIDID')->join('ts_book_gg on ts_book_gg.BOOK_ID = ts_book.ID')->join('ts_wz on ts_wz.GG_ID = ts_book_gg.ID')->join('ts_sg on ts_sg.BHCODE = ts_wz.WZ_ID')->join('ts_sj on ts_sg.SJ_ID = ts_sj.BHCODE')->join('ts_ck on ts_sj.CK_ID = ts_ck.BHCODE')->where($where)->find();
        return $data;
    }

    /*
     * 修改书籍内图
     */
    public function UpdatePic($ID, $save) {
        $this->db->startTrans();
        $OK = $this->db->where(array('BOOK_ID' => $ID))->select();
        $DELOK = 1;
        if ($OK) {
            $DELOK = $this->db->where(array('BOOK_ID' => $ID))->delete();
        }
        if ($DELOK) {
            $ADDOK = $this->db->addAll($save);
            if ($ADDOK) {
                $this->db->commit();
                return 1;
            }
        }
        $this->db->rollback();
        return 0;
    }

    /*
     * 移除书籍位置
     */
    public function ycwz($WZID) {
        $this->db->startTrans();
        $data = $this->db->field('ts_book.ID as BOOK_ID,
            ts_book.STOCK as BOOK_STOCK,
            ts_book_gg.ID as GG_ID,
            ts_book_gg.NUM as GG_NUM,
            ts_wz.ID as WZ_ID')->join('ts_book_gg on ts_book_gg.ID = ts_wz.GG_ID')->join('ts_book on ts_book_gg.BOOK_ID = ts_book.ID')->where(array('ts_wz.ID' => $WZID))->find();
        if ($data) {
            $OK1 = $this->db->where(array('ID' => $data['WZ_ID']))->delete();
            $this->db = M('book');
            $OK2 = $this->db->where(array('ID' => $data['BOOK_ID']))->save(array('STOCK' => ($data['BOOK_STOCK'] - 1)));
            $this->db = M('book_gg');
            $OK3 = $this->db->where(array('ID' => $data['GG_ID']))->save(array('NUM' => ($data['GG_NUM'] - 1)));
            if ($OK1 && $OK2 && $OK3) {
                $this->db->commit();
                return 1;
            }
        }
        $this->db->rollback();
        return 0;
    }

    /*
     * 添加位置单条位置
     * GG_ID 书籍规格ID
     * WZ_CODE 位置CODE
     */
    public function AddWz($GG_ID, $WZ_CODE) {
        $this->db->startTrans();
        //添加位置记录
        $save['GG_ID'] = $GG_ID;
        $save['WZ_ID'] = $WZ_CODE;
        $OK1 = $this->db->add($save);
        if ($OK1) {
            //修改规格库存
            $this->db = M('book_gg');
            $GGDATA = $this->db->where(array('ID' => $GG_ID))->find();
            $save1['NUM'] = $GGDATA['NUM'] + 1;
            $OK2 = $this->db->where(array('ID' => $GG_ID))->save($save1);
            if ($OK2) {
                //修改书籍库存
                $this->db = M('book');
                $BOOKDATA = $this->db->where(array('ID' => $GGDATA['BOOK_ID']))->find();
                $save2['STOCK'] = $BOOKDATA['STOCK'] + 1;
                $OK3 = $this->db->where(array('ID' => $GGDATA['BOOK_ID']))->save($save2);
                if ($OK3) {
                    $this->db->commit();
                    return 1;
                }
            }
        }
        $this->db->rollback();
        return 0;


    }


    /*
     * 查看书籍规格
     * 无规格则插入
     */
    public function findBookGG($where, $book_data) {
        $money = $book_data['Y_PRICE'];
        $have = $this->db->where($where)->find();
        //无规格插入
        if (!$have) {
            if ($where['GG_NAME'] == '二手书') {
                if ($book_data['PACKING_SPECIFICATION'] == '精装' || $book_data['PACKING_SPECIFICATION'] == '精装-套装') {
                    $money = $money * 0.45;
                } else {
                    $money = $money * 0.35;
                }
            } else {
                $money = $money * 0.7;
            }
            $save = $where;
            $save['NUM'] = 0;
            $save['GG_PRICE'] = $money;
            $OK = $this->db->add($save);
            if ($OK) {
                $save['ID'] = $OK;
                $res = $save;
            } else {
                $res = -1;
            }
        } else {
            $res = $have;
        }
        return $res;


    }

}


?>